<template>
  <work-template v-model="detailForm" @submitOn="submitOn" :flowId="flowId"
                 @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form" class="containerDiv">
      <repair-basice-info v-model="detailForm" :state="isEdit" ref="refRepairBasiceInfo"/>
      <repair-dstail v-model="detailForm" ref="refDetailForm" :state="isEdit"/>

    </div>
  </work-template>
</template>

<script>
import {
  addMaintenance,
  updateMaintenance,
  getMaintenance,
  getFindCarWorkOrderInfoVo,
} from "@/api/cwgd/maintenanceApi.js";
import {getWorkOrder} from "@/api/cwgd/cwgdApi.js";

import repairBasiceInfo from "../components/repair/repairBasiceInfo.vue";
import repairDstail from "../components/repair/repairDstail.vue"

export default {
  name: "MaintenanceDetails",
  components: {
    repairBasiceInfo, repairDstail
  },

  data() {
    return {
      maintenanceId: "", // 维修id

      isForm: true,
      method: {},
      information: {
        vinNo: "",
        carNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        belongCityName: "",
        orderNo: "",
        leaseNum: "1",
        customerName: "",
        leaseStartTime: "",
        leaseEndTime: "",
        totalBudgetPrice: "",

        registerTime: "",
        bicycleBudgetPrice: "",
        replacePrice: "",
        carSurplusPrice: "",
      },
      // 表单参数
      detailForm: {
        maintenanceId: "",
        carId: "",
        travelMeligile: "",
        workId: "",
        expectStartTime: "",
        expectEndTime: "",
        maintenanceType: "1",
        // 一般维修字段
        maintenanceShop: "",
        faultPhenomenon: "",
        maintenanceFaultDiagnosis: "",
        maintenanceQuoter: "",
        reasonForClaim: "",
        claimantReason: "",
        swappingAccessories: "",
        comprehensiveRecommendations: "",
        generalMaintenanceRemark: "",
        carMaintenanceProjectList: [],
        maintenanceProjectUrl: "",

        // 报废/出险
        insuredName: "",
        driverName: "",
        responsibleUnitName: "",
        responsibilityType: "",
        responsibleInsuranceUnitName: "",
        accidentDate: "",
        accidentType: "",
        accidentLocation: "",
        humanInjury: "",
        reportingCaseTime: "",
        lossAssessmentUrl: "",
        reportingCaseNum: '',

        vehicleAssessmentPrice: "",
        carOther: "",
        residualValue: "",
        residualUrl: "",
        assessmentTotalPrice: "",
        auctionCompanyName: "",
        bankAccount: "",
        threeLossAssessmentDtoList: [
          {
            personPrice: "",
            rescueCost: "",
            damagePrice: "",
            otherAssessmentPrice: "",
            otherCarNo: "",
            threeOther: "",
          },
        ],

        otherPrice: "",
        otherCostReason: "",
        otherCostUrl: "",

        advancePayment: "",
        advancePaymentCost: "",
        dangerMaintenanceShop: "",
        assessmentRemark: "",

        settlementType: "",
        accidentTime: "",
        compulsoryPolicyNum: "",
        commercialPolicyNum: "",
        depreciationPrice: "",
        depreciationPlan: "",
        depreciationPlanId:"",
        useSealsOss: "",
        lossAssessmentOss: "",
        otherAssessmentPrice: "",
        persionAssessmentPrice: "",
        accidentPhotos: "",
        compensationPaymentDto: [
          {
            payee: "",//收款方
            payAmount: "",//   收款费用
            collectionAccount: "", //收账号
          }
        ],
        compensationPaymentTotalAmount: '',
        actualStartTime: "",
        addAmountNun: "",
        actualEndTime: "",
        newPurchasePrice: "",
        accidentsNumber: "",
        chargingFees: "",
        fullCollectionFlag: "",
        insurancePayment: "",
        payType: "",
        maintenanceCompleteUrl: "",
        maintenanceCertificate: "",
        maintenanceFeedback: "",
      },


    };
  },
  watch: {},
  computed: {
    isShow() {
      return true;
    },
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId === '{flowId}' ? '' : this.$route.query.flowId
      }
    },
    isClass() {
      if (this.$route.query.flowId) {
        return true
      } else {
        return false
      }
    }
  },
  created() {
    this.method = this.$route.query;
    this.detailForm.carId = this.$route.query.carId;
    this.handleGetFindCarWorkOrderInfoVo()
    if (this.$route.query.businessDataId && this.$route.query.businessDataId !== 'null') {
      this.getMaintenance();
    }

  },
  mounted() {

  },
  methods: {
    handleGetFindCarWorkOrderInfoVo() {
      getFindCarWorkOrderInfoVo(this.$route.query.id).then(res => {
        if (res.code === 200) {
          for (let key in res.data) {
            this.$set(this.detailForm, key, res.data[key]);
          }
          this.detailForm.workId = res.data?.workId || this.$route.query.id
        }
      })
    },


    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/cwgd/maintenanceList",
        });
      });
    },

    // 提交操作
    submitOn() {
      let arr = [
        this.$refs.refRepairBasiceInfo.handleEffect(),
        this.$refs.refDetailForm.handleEffect(),
      ]
      Promise.all(arr).then(() => {
        const loading = this.$loading({
          lock: true,
          text: "保存中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        this.detailForm.workId = this.method.id;
        addMaintenance(this.detailForm).then((res) => {
          if (res.code === 200) {
            loading.close();
            this.closeSelectedTag(this.$route);
          }
        }).catch(() => {
          loading.close();
        });
      }).catch(() => {
      })

    },

    // 详情回显
    getMaintenance() {
      let id = this.method.businessDataId;

      getMaintenance(id).then((res) => {
        if (res.code === 200) {
          this.isForm = true;
          this.detailForm = res.data;
          this.detailForm.workId = this.$route.query.id
          this.detailForm.businessDataId = this.$route.businessDataId;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.containerDiv {
  height: 100%;
  display: flex;
  flex-direction: column;
}

::v-deep .container-div > .box-card:last-child {
  flex: 1;
  box-sizing: border-box;
}
</style>
